//
//  HWTweetCell.m
//  HomeWork
//
//  Created by apple on 2017/10/25.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HWTweetCell.h"
#import "HWTweetImageV.h"
#import "HWTweetContentV.h"
#import "HWTweetcommentView.h"

#import "HWTweetCellViewModel.h"
#import "HWTweetsModel.h"
@interface HWTweetCell()
/** 用户头像 */
@property (strong, nonatomic) UIImageView *iconV;
/** 用户名称 */
@property (strong, nonatomic) UILabel *nameL;
/** 发布时间 */
@property (strong, nonatomic) UILabel *timeL;
/** 评论按钮 */
@property (strong, nonatomic) UIButton *commentBtn;
/** 发布主内容 */
@property (strong, nonatomic) HWTweetContentV *contentV;
/** 发布图片 */
@property (strong, nonatomic) HWTweetImageV *tweetImageV;
/** 评论信息 */
@property (strong, nonatomic) HWTweetcommentView *commentV;


@end

@implementation HWTweetCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *identifier = @"HWTweetCell";
    HWTweetCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[HWTweetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubviews];
        self.contentView.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setupSubviews{
    
    UIView *sepreatV = [[UIView alloc] init];
    sepreatV.backgroundColor = UIColorFromRGB(0xdadada);
    [self.contentView addSubview:sepreatV];
    [sepreatV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.bottom.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    
    UIImageView *iconV = [[UIImageView alloc] init];
    self.iconV = iconV;
    [self.contentView addSubview:iconV];
    
    UILabel *nameL = [[UILabel alloc] init];
    self.nameL = nameL;
    [self.contentView addSubview:nameL];
    nameL.chainFont(FONT(17)).chainTextColor(Color333333);
    
    UILabel *timeL = [[UILabel alloc] init];
    self.timeL = timeL;
    [self.contentView addSubview:timeL];
    timeL.chainFont(FONT(11)).chainTextColor(Color333333);
    
    UIButton *commentBtn = [[UIButton alloc] init];
    self.commentBtn = commentBtn;
    [self.contentView addSubview:commentBtn];
    commentBtn.chainTitleForFont(FONT(13)).chainTitleColorForNormal(Color333333).chainImageNameForNormal(@"AlbumOperateMore");
    [commentBtn addTarget:self action:@selector(commentClick:) forControlEvents:UIControlEventTouchUpInside];
    
    HWTweetContentV *contentV = [[HWTweetContentV alloc] init];
    self.contentV = contentV;
    [self.contentView addSubview:contentV];
    
    HWTweetImageV *tweetImageV = [[HWTweetImageV alloc] init];
    self.tweetImageV = tweetImageV;
    [self.contentView addSubview:tweetImageV];
    
    HWTweetcommentView *commentV = [[HWTweetcommentView alloc] init];
    self.commentV = commentV;
    commentV.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:commentV];
    
    
}



- (void)updateConstraints{
    
    [self.iconV mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(homeScale(15));
        make.width.height.mas_equalTo(homeScale(30));
    }];
    
    [self.nameL mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconV.mas_right).offset(homeScale(8));
        make.top.mas_equalTo(self.iconV.mas_top);
        make.width.mas_lessThanOrEqualTo(200);
        make.height.mas_equalTo(17);
    }];
    
    if (!self.contentV.isHidden) { // 有文字
        [self.contentV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.nameL);
            make.right.mas_equalTo(-homeScale(20));
            make.top.mas_equalTo(self.nameL.mas_bottom).offset(homeScale(8));
        }];
    }
    
    if (!self.tweetImageV.isHidden) {
        if (!self.contentV.isHidden) { // 有图片和文字
            [self.tweetImageV mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.nameL);
                make.right.mas_equalTo(-homeScale(40));
                make.top.mas_equalTo(self.contentV.mas_bottom).offset(homeScale(8));
            }];
        } else {
            [self.tweetImageV mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.nameL);
                make.right.mas_equalTo(-homeScale(40));
                make.top.mas_equalTo(self.nameL.mas_bottom).offset(homeScale(8));
            }];
        }
    }
    
    if (self.tweetImageV.isHidden) {
        [self.timeL mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.nameL);
            make.top.mas_equalTo(self.contentV.mas_bottom).offset(homeScale(8));
            make.height.mas_equalTo(11);
            make.width.mas_lessThanOrEqualTo(200);
            
        }];
    } else {
        [self.timeL mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.nameL);
            make.top.mas_equalTo(self.tweetImageV.mas_bottom).offset(homeScale(8));
            make.height.mas_equalTo(11);
            make.width.mas_lessThanOrEqualTo(200);
            
        }];
    }
    
    [self.commentBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-homeScale(10));
        make.centerY.mas_equalTo(self.timeL);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
    }];
    
    if (self.model.comments.count) {
        [self.commentV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.nameL);
            make.right.mas_equalTo(-homeScale(10));
            make.top.mas_equalTo(self.timeL.mas_bottom).offset(homeScale(8));
        }];
    }
    
    [super updateConstraints];
    
}

- (void)setModel:(HWTweetsModel *)model{
    _model = model;
    self.tweetImageV.images = model.images;
    self.contentV.content = model.content;
    self.commentV.comments = model.comments;
    
    [self.iconV setImageWithUrl:model.sender.avatar placeholderImage:PlaceImage];
    self.nameL.chainText(model.sender.username);
    self.timeL.chainText(@"12分钟前");
    
    self.contentV.hidden = model.content.length > 0 ? NO : YES;
    self.tweetImageV.hidden = model.images.count > 0 ? NO : YES;
    self.commentV.hidden = model.comments.count > 0 ? NO : YES;

    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
}

- (void)setViewModel:(HWTweetCellViewModel *)viewModel{
    _viewModel = viewModel;
    self.commentV.viewModel = viewModel.commentViewModel;
    self.tweetImageV.viewModel = viewModel.imageViewModel;
}

- (void)commentClick:(UIButton *)btn{
    [self.viewModel.commentSubject sendNext:self.indexPath];
}


+ (CGFloat)heightWithModel:(HWTweetsModel *)model{
    
    if (!model.rowHeight) {
        HWTweetCell *cell = [[HWTweetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        cell.model = model;
        
        [cell layoutIfNeeded];
        
        CGRect frame;
        if (model.comments.count) {
            frame =  cell.commentV.frame;
            
        } else {
            frame =  cell.timeL.frame;
        }
        model.rowHeight = frame.origin.y + frame.size.height + homeScale(15);
        
        return model.rowHeight;
    } else {
        return model.rowHeight;
    }
}

@end
